<?php

/*
 * Copyright 2008 the original author or authors.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#namespace tehframework\objects\propertyeditors;

require_once 'test/testHelper.php';

class BooleanEditorTest extends BaseEditorTestCase
{
	/**
	 * @var BooleanEditor
	 */
	protected $e;
	
	protected function createEditor()
	{
		return new BooleanEditor();
	}
	
	function testConstructSetsGivenFalseStrings()
	{
		$e = new BooleanEditor(array('nope'));
		
		$this->assertFalse($e->editProperty('nope'));
	}

	function testCanEditPropertyReturnsTrueForAnyType()
	{
		$h = fopen(__FILE__, 'r');
		
		try
		{
			$this->assertTrue($this->e->canEditProperty($h));
		}
		catch (Exception $x)
		{
			fclose($h);
			
			throw $x;
		}
		
		$propertyValues = array(
			1, 1.1, 'foo', new stdClass, $this, true, false, null, array(1, 'two'));
		
		foreach ($propertyValues as $propertyValue)
		{
			$this->assertTrue($this->e->canEditProperty($propertyValue));
		}
	}
	
	function testSetFalseStrings()
	{
		$this->e->setFalseStrings(array('nop', ''));
		
		$this->assertFalse($this->e->editProperty('nop'));
		$this->assertFalse($this->e->editProperty(''));
	}
	
	function testEditPropertyUsesFalseOffNo0AndEmptyStringAsFalseStringsByDefault()
	{
		$this->assertFalse($this->e->editProperty('false'));
		$this->assertFalse($this->e->editProperty('off'));
		$this->assertFalse($this->e->editProperty('no'));
		$this->assertFalse($this->e->editProperty('0'));
		$this->assertFalse($this->e->editProperty(''));
	}
	
	function testEditPropertyFalseStringsComparisonIsCaseInsensitive()
	{
		$this->assertFalse($this->e->editProperty('FALSE'));
		$this->assertFalse($this->e->editProperty('oFF'));
		$this->assertFalse($this->e->editProperty('No'));
	}
	
	function testEditPropertyReturnsTrueIfSpecifiedValueIsStringDifferentThanAnyOfFalseStrings()
	{
		$this->assertTrue($this->e->editProperty('ok'));
		$this->assertTrue($this->e->editProperty('true'));
		$this->assertTrue($this->e->editProperty('foo'));
	}
	
	function testEditPropertyReturnsTrueForAnyStringValueIfNoFalseStringsWereDefined()
	{
		$this->e->setFalseStrings(array());
		
		$this->assertTrue($this->e->editProperty('ok'));
		$this->assertTrue($this->e->editProperty('true'));
		$this->assertTrue($this->e->editProperty('foo'));
		$this->assertTrue($this->e->editProperty('0'));
		$this->assertTrue($this->e->editProperty(''));
		$this->assertTrue($this->e->editProperty('false'));
	}
	
	function testEditPropertyReturnsTrueIfSpecifiedValueIsObject()
	{
		$this->assertTrue($this->e->editProperty($this));
	}
	
	function testEditPropertyReturnsTrueIfSpecifiedValueIsArray()
	{
		$this->assertTrue($this->e->editProperty(array(1, 2, 3)));
	}
	
	function testEditPropertyReturnsTrueIfSpecifiedValueIsEmptyArray()
	{
		$this->assertTrue($this->e->editProperty(array()));
	}
	
	function testEditPropertyReturnsTrueIfSpecifiedValueIsResource()
	{
		$this->assertTrue($this->e->editProperty(array(1, 2, 3)));
	}
	
	function testEditPropertyReturnsTrueIfSpecifiedValueIsNonZeroInteger()
	{
		$this->assertTrue($this->e->editProperty(1));
		$this->assertTrue($this->e->editProperty(-1));
		$this->assertTrue($this->e->editProperty(10));
	}
	
	function testEditPropertyReturnsTrueIfSpecifiedValueIsNonZeroFloat()
	{
		$this->assertTrue($this->e->editProperty(1.0));
		$this->assertTrue($this->e->editProperty(-1.1));
		$this->assertTrue($this->e->editProperty(10.01));
	}
	
	function testEditPropertyReturnsFalseIfSpecifiedValueIsInteger0()
	{
		$this->assertFalse($this->e->editProperty(0));
	}
	
	function testEditPropertyReturnsFalseIfSpecifiedValueIsFloat0()
	{
		$this->assertFalse($this->e->editProperty((float)0));
	}
	
	function testEditPropertyReturnsFalseIfSpecifiedValueIsNull()
	{
		$this->assertFalse($this->e->editProperty(null));
	}
	
	function testEditPropertyReturnsSpecifiedValueIfItsTypeIsBoolean()
	{
		$this->assertTrue($this->e->editProperty(true));
		$this->assertFalse($this->e->editProperty(false));
	}
}